/*eslint-disable*/

async function FileDataReader(progressEvent, dialogConfigure, initOptions, Encodes) {
    console.warn("警告，此函数只能打开 2 GB 大小以下的文件。");
    console.warn("Warning, this function can only open files smaller than 2 GB.");
    let OperateStatus;
    let FinalValue;
    let ReaderPromiseProcess = new Promise((resolve, reject) => {
        if (Object.prototype.toString.call(dialogConfigure) !== "[object Object]") {
            dialogConfigure = undefined;
        }
        window.showOpenFilePicker(dialogConfigure).then(val => {
            val[0].getFile().then(val2 => {
                if (String(val2.size).length >= 10 && val2.size >= 2000000000) {
                    reject("Maximum size limit exceeded.超出了最大文件大小限制");
                } else {
                    let tempReader = new FileReader();
                    switch (String(initOptions).toLowerCase()) {
                        case "binary":
                            tempReader.readAsBinaryString(val2);
                            break;
                        case "text":
                            if (typeof Encodes === "string") {
                                tempReader.readAsText(val2, String(Encodes).toString());
                            } else {
                                tempReader.readAsText(val2);
                            }
                            break;
                        case "dataurl":
                            tempReader.readAsDataURL(val2);
                            break;
                        case "arraybuffer":
                            tempReader.readAsArrayBuffer(val2);
                            break;
                        default:
                            tempReader.readAsBinaryString(val2);
                            break;
                    }
                    tempReader.onloadend = () => {
                        resolve(tempReader.result);
                    }
                    tempReader.onabort = (msg) => {
                        reject(msg);
                    }
                    tempReader.onerror = () => {
                        reject(tempReader.error);
                    }
                    tempReader.onprogress = (msg) => {
                        if (typeof progressEvent === "function") {
                            progressEvent(msg);
                        }
                    }
                }
            }).catch(val2 => {
                reject(val2);
            });
        }).catch(val => {
            reject(val);
        })
    });
    await ReaderPromiseProcess.then(value => {
        OperateStatus = true;
        FinalValue = value;
    }).catch(value => {
        OperateStatus = false;
        FinalValue = value
    });
    if (OperateStatus) {
        return FinalValue;
    } else {
        throw FinalValue;
    }
}

export {FileDataReader};